package cn.stylefeng.guns.modular.api.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.api.entity.Kpi;
import cn.stylefeng.guns.modular.api.mapper.KpiMapper;
import cn.stylefeng.guns.modular.api.model.params.KpiParam;
import cn.stylefeng.guns.modular.api.model.result.KpiResult;
import  cn.stylefeng.guns.modular.api.service.KpiService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 绩效 服务实现类
 * </p>
 *
 * @author wusonghe
 * @since 2019-08-31
 */
@Service
public class KpiServiceImpl extends ServiceImpl<KpiMapper, Kpi> implements KpiService {

    @Override
    public void add(KpiParam param){
        Kpi entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(KpiParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(KpiParam param){
        Kpi oldEntity = getOldEntity(param);
        Kpi newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public KpiResult findBySpec(KpiParam param){
        return null;
    }

    @Override
    public List<KpiResult> findListBySpec(KpiParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(KpiParam param){
        Page pageContext = getPageContext();
        if (pageContext.getSize()==0) {
            List datas =  this.baseMapper.customList(param);
            return LayuiPageFactory.createDataInfo(datas);
        }
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(KpiParam param){
        return param.getKpiId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private Kpi getOldEntity(KpiParam param) {
        return this.getById(getKey(param));
    }

    private Kpi getEntity(KpiParam param) {
        Kpi entity = new Kpi();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
